<?php
namespace app\admin\controller;
use app\admin\model\Adcate as AdcateModel;
use app\admin\controller\Common;
class Adcate extends Common
{

    public function lst()
    {   $auth=new Auth();
        $adcate=new AdcateModel();
        if(request()->isPost()){
            $sorts=input('post.');
            foreach ($sorts as $k => $v) {
                $adcate->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('lst'));
            return;
        }
        $adcates=$adcate->order('sort desc')->paginate(10);
        $this->assign('adcates',$adcates);
        return view();
	}


	    public function add(){
        if(request()->isPost()){
            $data=input('post.');
            $data['sort']=0;
            //print_r($data);
            $validate = \think\Loader::validate('Adcate');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $add=db('adcate')->insert($data);
            if($add){
                $this->success('添加广告分类成功！',url('lst'));
            }else{
                $this->error('添加广告分类失败！');
            }
        }
        return view();
    }

    public function edit(){
        if(request()->isPost()){
            $data=input('post.');
            $validate = \think\Loader::validate('Adcate');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $adcate=new AdcateModel();
            $save=$adcate->save($data,['id'=>$data['id']]);
            if($save !== false){
                $this->success('修改广告分类成功！',url('lst'));
            }else{
                $this->error('修改广告分类失败！');
            }
            return;
        }
        $adcate=AdcateModel::find(input('id'));
        $this->assign('adcate',$adcate);
        return view();
    }

    public function del(){
        $del=AdcateModel::destroy(input('id'));
        if($del){
           $this->success('删除广告分类成功！',url('lst')); 
        }else{
            $this->error('删除广告分类失败！');
        }
    }

    

}
